/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI.bll;

import BLL.establishTables;
import BLL.loginRegistration;

import javax.swing.*;
import java.io.File;

import static java.lang.String.valueOf;

/**
 *
 * @author YELIUHUISHI
 */
public class Login extends javax.swing.JFrame {
    Thread timeThread =new Thread();
    /**
     * Creates new form login
     */
    public Login() {
        initComponents();
        init();
    }

    private void init() {
        new establishTables().inspectLogin();
        this.setTitle("登录界面");
        this.setResizable(false);
        uiutlLogin uiutlLogin = new uiutlLogin();
        uiutlLogin.setFrameConter(this);
        uiutlLogin.setFrameTmage(this,"src"+File.separator+ "UI" +File.separator+"icon"+File.separator+"login.png");
        timeThread = new Thread(new runnableTime(this.Time));
        timeThread.start();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Time= new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        loginName = new javax.swing.JTextField();
        loginPassword = new javax.swing.JPasswordField();
        loginLogin = new javax.swing.JButton();
        loginRegister = new javax.swing.JButton();
        loginEixt = new javax.swing.JButton();

        Time.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        Time.setForeground(new java.awt.Color(255, 20, 18));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("用户：");
        loginName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginNameActionPerformed(evt);
            }
        });

        jLabel3.setText("密码：");
        loginPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginLoginActionPerformed(evt);
            }
        });

        loginLogin.setText("登录");
        loginLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginLoginActionPerformed(evt);
            }
        });

        loginRegister.setText("注册");
        loginRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginRegisterActionPerformed(evt);
            }
        });

        loginEixt.setText("退出");
        loginEixt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginEixtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(loginLogin)
                                .addGap(53, 53, 53)
                                .addComponent(loginRegister))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(loginName)
                                    .addComponent(loginPassword)))
                            .addComponent(Time, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(loginEixt)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(loginName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(loginPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginLogin)
                    .addComponent(loginRegister))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginEixt)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //用户框的动作事件
    private void loginNameActionPerformed(java.awt.event.ActionEvent evt){
        this.loginPassword.requestFocus();
    }

    //登录键的动作事件
    private void loginLoginActionPerformed(java.awt.event.ActionEvent evt) {
        //获取输入的用户已经密码
        String userName= this.loginName.getText().trim();
        String userPassword = valueOf(this.loginPassword.getPassword());

        //用户名规则
        String userNameRegex="[A-Za-z]{3,20}";
        String userPasswordRegex="\\w{6,20}";

        //校验
        if(!userName.matches(userNameRegex)){
            JOptionPane.showMessageDialog(this,"用户名不规范（3个以上英文字母组成）");
            this.loginName.setText("");
            this.loginName.requestFocus();
            return;
        }
        if(!userPassword.matches(userPasswordRegex)){
            JOptionPane.showMessageDialog(this,"密码不满足要求(6到20个任意单词字符)");
            this.loginPassword.setText("");
            this.loginPassword.requestFocus();
            return;
        }

        loginRegistration loginRegistration = new loginRegistration();
        Boolean longing = loginRegistration.longing(userName, userPassword);
        if(!longing){
            JOptionPane.showMessageDialog(this,"登录失败");
            this.loginPassword.setText("");
            this.loginPassword.requestFocus();
            return;
        }
        timeThread.stop();
        new Homepage().setVisible(true);
        this.dispose();
    }

    //注册键的动作事件
    private void loginRegisterActionPerformed(java.awt.event.ActionEvent evt) {
        timeThread.stop();
        new Register().setVisible(true);
        this.dispose();
    }
    //退出键的动作事件
    private void loginEixtActionPerformed(java.awt.event.ActionEvent evt) {
        timeThread.stop();
        System.exit(0);
    }

    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    public javax.swing.JButton loginEixt;
    public javax.swing.JButton loginLogin;
    public javax.swing.JTextField loginName;
    public javax.swing.JPasswordField loginPassword;
    public javax.swing.JButton loginRegister;
    public javax.swing.JLabel Time;
}
